package cn.kgc.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2023年01月05日11:33
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Exchange stockExchange(){
        return new TopicExchange("stock.exchange",true,false);
    }

    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> arguments=new HashMap<>();
        arguments.put("x-message-ttl",12000);   //超时时间
        arguments.put("x-dead-letter-exchange","stock.exchange");  //死信交换机
        arguments.put("x-dead-letter-routing-key","unlock.stock"); //死信routingKey
        Queue queue=new Queue("stock.delay.queue",true,
                true,false,arguments);
        return queue;
    }

    @Bean
    public Queue stockUnlockQueue(){
        return new Queue("stock.unlock.queue",true,
                true,false);
    }

    @Bean
    public Binding stockDelayQueueBinding(){
        Binding binding=new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,"stock.exchange",
                "lock.stock",null);
        return binding;
    }

    @Bean
    public Binding stockUnlockQueueBinding(){
        Binding binding=new Binding("stock.unlock.queue",
                Binding.DestinationType.QUEUE,"stock.exchange",
                "unlock.stock",null);
        return binding;
    }

    //消息转换器
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
